package com.lingjtx.im.core.distribute;

import com.lingjtx.common.core.exception.ResponseErrorException;
import com.lingjtx.im.core.handle.ImMessageHandle;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class MessageTypeContext {

    private final List<ImMessageHandle> contexts = new ArrayList<>();

    public void setHandlers(ImMessageHandle handle) {
        contexts.add(handle);
    }

    public ImMessageHandle getHandle(byte type) {
        for (ImMessageHandle context : contexts) {
            if (context.getImType() == type) {
                return context;
            }
        }
        throw new ResponseErrorException(String.format("未知消息类型: %s！", type));
    }
}
